package com.yinhai.message.manage.register.dao;

import com.yinhai.core.common.api.base.IPage;
import com.yinhai.core.common.ta3.dto.TaParamDto;
import com.yinhai.core.service.ta3.domain.dao.HibernateDAO;
import com.yinhai.message.manage.register.po.AppConfigPo;
import com.yinhai.message.manage.register.vo.AppConfigVo;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
*
*@author lanyc
*@version created: 2017年4月5日上午10:30:17
*/
public class AppConfigDaoImpl extends HibernateDAO<AppConfigPo> implements IAppConfigDao {

	@Override
	public List<AppConfigVo> queryAllAppConfig() {
		StringBuilder hql = new StringBuilder();
        hql.append("select new com.yinhai.message.manage.register.vo.AppConfigVo(")
        .append("acp.defaultApp,")
        .append("acp.appKey,")
        .append("acp.masterSecret,")
        .append("acp.sid,")
        .append("acp.systemId,")
        .append("acp.systemName )")
        .append("from com.yinhai.message.manage.register.po.AppConfigPo acp ");
        return super.selectFromMultiTable(hql.toString());
	}

	@Override
	public Integer addAppConfig(AppConfigPo acp) {
		save(acp);
		return null;
	}

	@Override
	public Integer updateAppConfig(AppConfigPo po) {
		StringBuilder hql = new StringBuilder("update AppConfigPo po set");
		List<Object> objs = new LinkedList<>();
		if(!ObjectUtils.isEmpty(po.getId())){
			hql.append(" po.id = ?,");
			objs.add(po.getId());
		}
		if(!ObjectUtils.isEmpty(po.getDefaultApp())){
			hql.append(" po.defaultApp = ?,");
			objs.add(po.getDefaultApp());
		}
		if(po.getSystemId()!=null){
			hql.append(" po.systemId = ?,");
			objs.add(po.getSystemId());
		}
		if(po.getSystemName()!=null){
			hql.append(" po.systemName = ?,");
			objs.add(po.getSystemName());
		}
		if(po.getSid()!=null){
			hql.append(" po.sid = ?,");
			objs.add(po.getSid());
		}
		if(po.getTestRegistrationId()!=null){
			hql.append(" po.testRegistrationId = ?,");
			objs.add(po.getTestRegistrationId());
		}
		if(!ObjectUtils.isEmpty(po.getMasterSecret())){
			hql.append(" po.masterSecret = ?");
			objs.add(po.getMasterSecret());
		}
		if(!ObjectUtils.isEmpty(po.getAppKey())){
			hql.append(" where po.appKey = ?");
			objs.add(po.getAppKey());
		}
		update(hql.toString(),objs.toArray());
		return null;
	}

	@Override
	public Integer deleteAppConfig(String appKey) {
		delete("delete from AppConfigPo acp where acp.appKey=?", appKey);
		return null;
	}

	@Override
	public AppConfigVo queryDefaultAppConfig() {
		StringBuilder hql = new StringBuilder();
        hql.append("select new com.yinhai.message.manage.register.vo.AppConfigVo(")
        .append("acp.defaultApp,")
        .append("acp.appKey,")
        .append("acp.masterSecret,")
        .append("acp.sid,")
        .append("acp.systemId,")
        .append("acp.systemName )")
        .append("from com.yinhai.message.manage.register.po.AppConfigPo acp ")
        .append("where acp.defaultApp=1");
        return selectFiled(hql.toString());
	}

	@Override
	public AppConfigVo queryAppCfgByAppKey(String appKey) {
		StringBuilder hql = new StringBuilder();
        hql.append("select new com.yinhai.message.manage.register.vo.AppConfigVo(")
        .append("acp.defaultApp,")
        .append("acp.appKey,")
        .append("acp.masterSecret,")
        .append("acp.sid,")
        .append("acp.systemId,")
        .append("acp.systemName, ")
        .append("acp.testRegistrationId )")
        .append("from com.yinhai.message.manage.register.po.AppConfigPo acp ")
        .append("where acp.appKey=?");
        return selectFiled(hql.toString(),appKey);
	}

	@Override
	public IPage<AppConfigVo> queryAllAppConfigByPage(TaParamDto dto, int start, int limit, String string) {
		String appKey = dto.getAsString("qry_appKey");
		String systemName = dto.getAsString("qry_systemName");
		HashMap<String, Object> params = new HashMap<String, Object>();
		StringBuilder hql = new StringBuilder();
        hql.append("select new com.yinhai.message.manage.register.vo.AppConfigVo(")
        .append("acp.defaultApp,")
        .append("acp.appKey,")
        .append("acp.masterSecret,")
        .append("acp.sid,")
        .append("acp.systemId,")
        .append("acp.systemName )")
        .append("from com.yinhai.message.manage.register.po.AppConfigPo acp ")
        .append(" where 1=1 ");
        if (!StringUtils.isEmpty(appKey)) {
			params.put("appKey", "%" + appKey + "%");
			hql.append(" and acp.appKey like :appKey");
		}
        if(!StringUtils.isEmpty(systemName)){
			params.put("systemName", "%" + systemName + "%");
			hql.append(" and nvl(acp.systemName,' ') like :systemName");
		}
        return selectFromMultiTableWithPage(hql.toString(), params, start, limit, string);
	}

	@Override
	public void removeDefault() {
		delete("update AppConfigPo acp set acp.defaultApp=0 where acp.defaultApp=1");
	}

}
